import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/views/Main'
import cloneDeep from 'lodash/cloneDeep'
import store from '@/store'

Vue.use(Router)

// 页面路由(独立页面)
export const pageRoutes = [
  {
    path: '*',
    component: () => import('@/views/pages/404'),
    name: '404',
    meta: { title: '404未找到', type: '错误提示', legend: 'http://discover.zs.eenet.com   http://media.find.eenet.com 正' },
    beforeEnter (to, from, next) {
      // 拦截处理特殊业务场景
      // 如果, 重定向路由包含__双下划线, 为临时添加路由
      if (/__.*/.test(to.redirectedFrom)) {
        return next(to.redirectedFrom.replace(/__.*/, ''))
      }
      next()
    }
  },
  // 个人中心
  { path: '/personal-login', name: 'personal-login', component: () => import('@/views/modules/personal/login'), meta: { title: '绑定手机号', type: '个人中心', legend: '授权页面判断，未绑定手机号则跳此页面' } },
  { path: '/personal-student', name: 'personal-student', component: () => import('@/views/modules/personal/user-stu'), meta: { title: '学员中心', type: '个人中心', legend: '学员身份显示' } },
  { path: '/personal-tourist', name: 'personal-tourist', component: () => import('@/views/modules/personal/user-tourist'), meta: { title: '游客中心', type: '个人中心', legend: '未登录先到此页面，点击直接跳转授权' } },
  { path: '/personal-member', name: 'personal-member', component: () => import('@/views/modules/personal/user-member'), meta: { title: '会员中心', type: '个人中心', legend: '会员身份显示' } },
  { path: '/personal-authorize', name: 'personal-authorize', component: () => import('@/views/modules/personal/authorize'), meta: { title: '微信授权', type: '个人中心', legend: 'device=APP/WX/H5&gsign=gkzx&uniacid=25&logoUrl=logo地址&activityName=活动名&activityId=&pageH5=2' } },
  { path: '/personal-order', name: 'personal-order', component: () => import('@/views/modules/personal/order'), meta: { title: '我的订单', type: '个人中心', legend: '订单，收据，发票' } },
  { path: '/personal-order-detail', name: 'personal-order-detail', component: () => import('@/views/modules/personal/order-detail'), meta: { title: '我的订单-详情', type: '个人中心', legend: '查看订单详情进行缴费' } },
  { path: '/personal-coupon', name: 'personal-coupon', component: () => import('@/views/modules/personal/coupon'), meta: { title: '优惠券/补贴', type: '个人中心', legend: 'app访问需要携带 ?device=app' } },
  { path: '/personal-refund', name: 'personal-refund', component: () => import('@/views/modules/personal/refund'), meta: { title: '退费说明', type: '个人中心', legend: '静态页' } },
  // 学历教育补贴
  { path: '/subsidy-login', name: 'subsidy-login', component: () => import('@/views/modules/subsidy/login'), meta: { title: '工会职工专享福利', type: '学历教育补贴', legend: 'phone=1511111111&gsign=gkzx&device=app' } },
  { path: '/subsidy-list', name: 'subsidy-list', component: () => import('@/views/modules/subsidy/list'), meta: { title: '我的补贴券', type: '学历教育补贴', legend: '分享功能只在APP中有效' } },
  // 溢达助力计划首届毕业典礼
  { path: '/extra-index', name: 'extra-index', component: () => import('@/views/modules/extra/index'), meta: { title: '溢达助力计划首届毕业典礼', type: '溢达助力计划首届毕业典礼', legend: '推广静态页' } },
  // 报读页面
  { path: '/read-login', name: 'read-login', component: () => import('@/views/modules/read/login'), meta: { title: '快速报名', type: '报读页面', legend: '在线报读这个....' } },
  // 学期学情
  { path: '/score-semester', name: 'score-semester', component: () => import('@/views/modules/score/semester'), meta: { title: '学期学情', type: '学情/服务', legend: 'studentId=&gradeId= 学生ID 年级ID' } },
  { path: '/score-behavior', name: 'score-behavior', component: () => import('@/views/modules/score/behavior'), meta: { title: '本学期累计学习行为', type: '学情/服务', legend: 'studentId=&gradeId= 学生ID 年级ID' } },
  { path: '/score-list', name: 'score-list', component: () => import('@/views/modules/score/list'), meta: { title: '学期学情', type: '学情/服务', legend: 'studentId=&gradeId= 学生ID 年级ID' } },
  { path: '/score-specialty', name: 'score-specialty', component: () => import('@/views/modules/score/specialty'), meta: { title: '专业学情', type: '学情/服务', legend: '暂时不对接' } },
  // 会员权益
  { path: '/score-service', name: 'score-service', component: () => import('@/views/modules/score/service'), meta: { title: '服务介绍', type: '会员权益', legend: '?gsign=' } },
  { path: '/score-legend', name: 'score-legend', component: () => import('@/views/modules/score/legend'), meta: { title: '工会会员', type: '会员权益', legend: 'phone=' } },
  { path: '/score-verify', name: 'score-verify', component: () => import('@/views/modules/score/verify'), meta: { title: '会员核验申请', type: '会员权益', legend: '会员核验第一步' } },
  { path: '/score-voucher', name: 'score-voucher', component: () => import('@/views/modules/score/voucher'), meta: { title: '上传工会会员证', type: '会员权益', legend: '会员核验第二步' } },
  // 自媒体
  { path: '/media-find', name: 'media-find', component: () => import('@/views/modules/media/find'), meta: { keepAlive: true, title: '发现', type: '自媒体', legend: '?device=APP/WX/H5&gsign=渠道标识(gkzx)&uniacid=微信ID(渠道ID)&shareId=分享者ID&articleId=文章ID（app需要加openid）' } },
  { path: '/media-attention', name: 'media-attention', component: () => import('@/views/modules/media/attention'), meta: { keepAlive: true, title: '关注', type: '自媒体', legend: '' } },
  { path: '/media-attention-more', name: 'media-attention-more', component: () => import('@/views/modules/media/attention-more'), meta: { title: '发现兴趣', type: '自媒体', legend: '' } },
  { path: '/media-sarch', name: 'media-sarch', component: () => import('@/views/modules/media/sarch'), meta: { title: '搜索', type: '自媒体', legend: '' } },
  { path: '/media-personal', name: 'media-personal', component: () => import('@/views/modules/media/personal'), meta: { title: '个人空间', type: '自媒体', legend: '' } },
  { path: '/media-topic', name: 'media-topic', component: () => import('@/views/modules/media/topic'), meta: { title: '话题空间', type: '自媒体', legend: '' } },
  { path: '/media-detail', name: 'media-detail', component: () => import('@/views/modules/media/detail'), meta: { title: '文章详情', type: '自媒体', legend: '' } },
  { path: '/media-detail-no', name: 'media-detail-no', component: () => import('@/views/modules/media/detail-no'), meta: { title: '精选分享', type: '自媒体', legend: '' } },
  { path: '/media-detail-open', name: 'media-detail-open', component: () => import('@/views/modules/media/detail-open'), meta: { title: '加载中...', type: '自媒体', legend: '' } },
  { path: '/media-video', name: 'media-video', component: () => import('@/views/modules/media/video'), meta: { title: '视频详情', type: '自媒体', legend: '' } },
  // 选学
  { path: '/elective-choose', name: 'elective-choose', component: () => import('@/views/modules/elective/choose'), meta: { title: '选学-首页', type: '选学', legend: '?device=APP/WX/H5&school_id=学校ID&center_id=学习中心ID&gsign=gkzx&uniacid=25' } },
  { path: '/elective-detail', name: 'elective-detail', component: () => import('@/views/modules/elective/detail'), meta: { title: '专业详情', type: '选学', legend: '?id=产品ID&center_id=学习中心ID' } },
  { path: '/elective-list', name: 'elective-list', component: () => import('@/views/modules/elective/list'), meta: { title: '专业列表', type: '选学', legend: '' } },
  { path: '/elective-sarch', name: 'elective-sarch', component: () => import('@/views/modules/elective/sarch'), meta: { title: '搜索', type: '选学', legend: '' } },
  { path: '/elective-register', name: 'elective-register', component: () => import('@/views/modules/elective/register'), meta: { title: '报名', type: '选学', legend: '' } },
  // 用户中心
  { path: '/user-index', name: 'user-index', component: () => import('@/views/modules/user/index'), meta: { title: '个人中心', type: '用户中心', legend: '' } },
  { path: '/user-index-ios', name: 'user-index-ios', component: () => import('@/views/modules/user/index-ios'), meta: { title: '个人中心', type: '用户中心', legend: 'IOS审核专用' } },
  { path: '/user-phone', name: 'user-phone', component: () => import('@/views/modules/user/phone'), meta: { title: '绑定手机号', type: '用户中心', legend: '' } },
  { path: '/user-member', name: 'user-member', component: () => import('@/views/modules/user/member'), meta: { title: '个人中心', type: '用户中心', legend: '// 微信?device=WX&gsign=gkzx&uniacid=39// 手机?device=APP&phone=' } },
  { path: '/user-member-ios', name: 'user-member-ios', component: () => import('@/views/modules/user/member-ios'), meta: { title: '个人中心', type: '用户中心', legend: '// 微信?device=WX&gsign=gkzx&uniacid=39// 手机?device=APP&phone=' } },
  // 海报
  { path: '/poster-read', name: 'poster-read', component: () => import('@/views/modules/poster/read'), meta: { title: '报读缴费平台', type: '海报', legend: '' } },
  { path: '/poster-manager', name: 'poster-manager', component: () => import('@/views/modules/poster/manager'), meta: { title: '一部手机读完大学', type: '海报', legend: '?gsign=gkzx&school_code=407&learn_center_id=8038' } },
  { path: '/poster-intent', name: 'poster-intent', component: () => import('@/views/modules/poster/intent'), meta: { title: '初中高中学历也能读大学', type: '海报', legend: '?gsign=gkzx&school_code=407&learn_center_id=8038' } },
  // 分享有礼
  { path: '/share-index', name: 'share-index', component: () => import('@/views/modules/share/index'), meta: { title: '分享有礼', type: '分享有礼', legend: '?device=WX/APP&gsign=gkzx&uniacid=39&phone=15179236849' } },
  { path: '/share-receive', name: 'share-receive', component: () => import('@/views/modules/share/receive'), meta: { title: '分享有礼', type: '分享有礼', legend: '' } },
  { path: '/share-success', name: 'share-success', component: () => import('@/views/modules/share/success'), meta: { title: '分享有礼', type: '分享有礼', legend: '' } },
  { path: '/share-register', name: 'share-register', component: () => import('@/views/modules/share/register'), meta: { title: '分享有礼', type: '分享有礼', legend: '' } },
  // 民航介绍页
  { path: '/mh-index', name: 'mh-index', component: () => import('@/views/modules/mh/index'), meta: { title: '关于我校', type: '民航介绍页', legend: '' } },
  { path: '/mh-detail', name: 'mh-detail', component: () => import('@/views/modules/mh/detail'), meta: { title: '详情', type: '民航介绍页', legend: '' } }
]

// 模块路由(基于主入口布局页面)
export const moduleRoutes = {
  path: '/',
  name: 'main',
  component: Main,
  redirect: '/home', // 默认子路由
  children: [
    { path: '/home', name: 'home', component: () => import('@/views/modules/home'), meta: { title: 'H5页面' } }
  ]
}

const router = new Router({
  mode: 'history', // hash history
  scrollBehavior: () => ({ y: 0 }),
  routes: pageRoutes.concat(moduleRoutes)
})

// 赋值全局参数
window.SITE_CONFIG['menuList'] = cloneDeep(pageRoutes)

router.beforeEach((to, from, next) => {
  // 动态标题
  window.document.title = to.meta.title
  // 处理APP无法获取动态路由的问题
  if (store.state.device === 'APP') {
    if (from.path === '/media-find' || from.path === '/media-attention' ||
      from.path === '/elective-choose' || from.path === '/elective-list' ||
      from.path === '/elective-sarch') {
      // eslint-disable-next-line no-undef
      Phone.goNativeWeb(`http://${window.location.host}${to.fullPath}`)
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
